package io.renren.modules.member.controller;

import io.renren.common.utils.IPUtils;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.member.entity.V9MemberEntity;
import io.renren.modules.member.service.V9MemberService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Map;



/**
 * 会员管理
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2019-09-24 11:37:15
 */
@RestController
@RequestMapping("member/v9member")
public class V9MemberController {
    @Autowired
    private V9MemberService v9MemberService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("member:v9member:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = v9MemberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{userid}")
    @RequiresPermissions("member:v9member:info")
    public R info(@PathVariable("userid") Integer userid){
		V9MemberEntity v9Member = v9MemberService.getById(userid);
        return R.ok().put("v9Member", v9Member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("member:v9member:save")
    public R save(@RequestBody V9MemberEntity v9Member, HttpServletRequest request){

        String ip = IPUtils.getIpAddr(request);

        Integer phpssouid;
        //取最新一条数据,然后phpssouid +1
        phpssouid = v9MemberService.getPhpssouid();
        if (phpssouid != null){
            // 算出新的phpssouid 值
            phpssouid = phpssouid+1;
        }else{
            phpssouid = 1;
        }
        //重新把算的出来的phpssouid值set进实体类
        v9Member.setPhpssouid(phpssouid);
        v9Member.setRegip(ip);
		v9MemberService.save(v9Member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("member:v9member:update")
    public R update(@RequestBody V9MemberEntity v9Member){
		v9MemberService.updateById(v9Member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("member:v9member:delete")
    public R delete(@RequestBody Integer[] userids){
		v9MemberService.removeByIds(Arrays.asList(userids));

        return R.ok();
    }

}
